//
//  GAOEmoticonToolBar.swift
//  Weibo28
//
//  Created by 高辰 on 16/5/20.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

enum GAOEmoticonToolBarButtonType: Int {
    //  默认
    case Normal = 1000
    //  emoji
    case Emoji = 1001
    //  浪小花
    case Lxh = 1002
}

class GAOEmoticonToolBar: UIStackView {
    //记录上一次选中的按钮
    var lastSelectedButton: UIButton?
    //定义点击表情按钮的闭包
    var didSelectedEmoticonButtonClosure: ((type: GAOEmoticonToolBarButtonType) -> ())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI() {
        //  布局方式
        axis = .Horizontal
        //  填充方式
        distribution = .FillEqually
        
        addChildButton("默认", backgroundImageName: "compose_emotion_table_left",type: .Normal)
        addChildButton("Emoji", backgroundImageName: "compose_emotion_table_mid",type: .Emoji)
        addChildButton("浪小花", backgroundImageName: "compose_emotion_table_right",type: .Lxh)
        
    }
    
    
    private func addChildButton(title: String, backgroundImageName: String, type: GAOEmoticonToolBarButtonType) {
        let button = UIButton()
        button.tag = type.rawValue
        
        button.setTitle(title, forState: .Normal)
        //  设置文字颜色
        button.setTitleColor(UIColor.whiteColor(), forState: .Normal)
        button.setTitleColor(UIColor.grayColor(), forState: .Selected)
        //  字体大小
        button.titleLabel?.font = UIFont.systemFontOfSize(15)
        //  设置背景图片
        button.setBackgroundImage(UIImage(named: "\(backgroundImageName)_normal"), forState: .Normal)
        button.setBackgroundImage(UIImage(named: "\(backgroundImageName)_selected"), forState: .Selected)
        button.adjustsImageWhenHighlighted = false
        button.addTarget(self, action: "emoticonTypeButtonClick:", forControlEvents: .TouchUpInside)
        //  添加按钮
        addArrangedSubview(button)
        // 默认选中默认类型的button
        if type == .Normal {
            lastSelectedButton?.selected = false
            button.selected = true
            lastSelectedButton = button
        }
    }
    
    //  MARK:   - 点击事件
    @objc private func emoticonTypeButtonClick(button: UIButton) {
        
        //如果再次点击的按钮和上一次是同一个按钮,则不执行
        if button == lastSelectedButton {
            return
        }
        //  1.取消上一次选中的状态
        lastSelectedButton?.selected = false
        //  2.当前点击的按钮选中
        button.selected = true
        //  3.记录选中按钮
        lastSelectedButton = button
        
        let type = GAOEmoticonToolBarButtonType(rawValue: button.tag)!
        //执行闭包
        didSelectedEmoticonButtonClosure?(type: type)
    }
    
    /**
     选中表情类型的button
     
     - parameter section: cell的组
     */
    func selectEmoticonTypeButtonWithSection(section: Int) {
        if (section + 1000) == lastSelectedButton?.tag {
            return
        }
        
        let button = viewWithTag(1000 + section) as! UIButton
        lastSelectedButton?.selected = false
        button.selected = true
        lastSelectedButton = button
    }
    

}
